// packageI/period_pay/period_pay.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payId:'',
    paymentIndex:0,
    cashIndex:0,
    payActive:0,
    deductShow:false,
    status:"",
    isLoadMore: true,
    page: 1,
    totalPage: 0,
    iPnoneBottomBol:false, //是否是苹果手机
    paymentList:[
      {
        name:'全部',
        index:0,
        status:''
      },
      {
        name:'进行中',
        index:1,
        status:1
      },
      {
        name:'已完成',
        index:2,
        status:3
      },
      {
        name:'失效',
        index:3,
        status:-1
      }
    ],
    cashList: [
      {
        name:'全部',
        index:0,
        status:''
      },
      {
        name:'待结算',
        index:1,
        status:0
      },
      {
        name:'已结算',
        index:2,
        status:1
      },
      {
        name:'已失效',
        index:2,
        status:-1
      }
    ],
    paylogList:[],
    api:'plugin.alipay-period-deduct.frontend.index.pay-log',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(this.data.payActive === 0) {
      this.data.api = 'plugin.alipay-period-deduct.frontend.index.pay-log';
    } else {
      this.data.api = 'plugin.alipay-period-deduct.frontend.index.security-log';
    }
    this.paylogData();
    this.getiPnoneBottom();
  },
  getiPnoneBottom() {
    let that = this;
    try {
      let value = wx.getStorageSync('iPhoneBottom');
      if (value) {
        that.setData({
          iPnoneBottomBol: true
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  payChange(event) {
    this.setData({
      payActive:event.detail.name,
      status:'',
      cashIndex:0,
      paymentIndex:0,
      page:1,
      isLoadMore:true
    });
    if(this.data.payActive === 0) {
      this.data.api = 'plugin.alipay-period-deduct.frontend.index.pay-log';
    } else {
      this.data.api = 'plugin.alipay-period-deduct.frontend.index.security-log';
    }
    this.paylogData();
  },
  paymentOn(event) {
    let {index,status,type} = event.currentTarget.dataset;
    if(type === 0) {
      this.setData({
        paymentIndex:index
      })
    } else {
      this.setData({
        cashIndex:index
      })
    }
    this.setData({
      paymentIndex: index,
      page:1,
      status:status
    })
    this.paylogData();
  },
  recordClick(event) {
    this.setData({
      payId: event.detail.id,
      deductShow: true
    })
    this.deductionLog();
  },
  deductClose(event) {
    this.setData({
      deductShow:event.detail
    })
  },
  deductionLog() { //扣款记录
    let urlStr = app.getNetAddresss('plugin.alipay-period-deduct.frontend.index.deduction-log');
    app._postNetWork({
      url: urlStr,
      data:{id:this.data.payId},
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let list = res.data?.list;
          this.setData({
            deductionData:list
          })
        } else {
          this.data.query.page = this.data.query.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  paylogData() {
    let params = {
      status:this.data.status,
      page:this.data.page,
      basic_info:1
    };
    let urlStr = app.getNetAddresss(this.data.api);
    app._postNetWork({
      url: urlStr,
      data:params,
      success:  (resdata) => {
        let res = resdata.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          let list = res.data.list;
          this.data.totalPage = list.last_page;
          let basicInfo = res.basic_info;
          this.data.isLoadMore = true;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            paylogList: list.data,
            siteName:basicInfo?.home?.mailInfo?.name,
            imageUrl:basicInfo?.home?.mailInfo?.logo_url,
          })
        } else {
          this.data.page = this.data.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
   //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      if(this.data.payActive === 0) {
        this.data.api = 'plugin.alipay-period-deduct.frontend.index.pay-log';
      } else {
        this.data.api = 'plugin.alipay-period-deduct.frontend.index.security-log';
      }
      this.data.page = this.data.page + 1;
      let params = {
        status:this.data.status,
        page:this.data.page,
        basic_info:1
      };
      let urlStr = app.getNetAddresss(this.data.api);
      app._getNetWork({
        url: urlStr,
        data:params,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.list?.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              paylogList : this.data.paylogList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      status:'',
      page:1,
      paymentIndex:0,
      cashIndex:0,
      payActive:0,
    })
    this.paylogData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.siteName,
      imageUrl:this.data.imageUrl,
      path: `/packageI/period_pay/period_pay`
    };
  },
  onShareTimeline() {
    return {
      title: this.data.siteName,
      imageUrl:this.data.imageUrl,
      path: `/packageI/period_pay/period_pay`
    };
  }
})